package org.example.shardiing;

import org.junit.platform.commons.util.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.Locale;

/**
 * @author zengxiaoyu
 * @date 2025/7/11 9:19
 */
public class DbTemplate {

    protected JdbcTemplate jdbcTemplate;
    private static final String POSTGRESQL_URL_PREFIX = "jdbc:postgresql://";

    private static final String MYSQL_URL_PREFIX = "jdbc:mysql://";

    public DbTemplate(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public static boolean isPostgresql(String url) {
        if(StringUtils.isNotBlank(url) && url.toLowerCase(Locale.ROOT).startsWith(POSTGRESQL_URL_PREFIX)){
            return true;
        }
        return false;
    }

    public static boolean isMysql(String url) {
        if(StringUtils.isNotBlank(url) && url.toLowerCase(Locale.ROOT).startsWith(MYSQL_URL_PREFIX)){
            return true;
        }
        return false;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

}
